/*
 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "wfd_js_result.h"
#include "common/sharing_log.h"

namespace OHOS {
namespace Sharing {

static bool SetPropertyInt32(napi_env env, napi_value &obj, const std::string &key, int32_t value)
{
    SHARING_LOGD("trace.");
    CHECK_AND_RETURN_RET(obj != nullptr, false);
    napi_value keyNapi = nullptr;

    napi_status status = napi_create_string_utf8(env, key.c_str(), NAPI_AUTO_LENGTH, &keyNapi);
    CHECK_AND_RETURN_RET(status == napi_ok, false);

    napi_value valueNapi = nullptr;
    status = napi_create_int32(env, value, &valueNapi);
    CHECK_AND_RETURN_RET(status == napi_ok, false);

    status = napi_set_property(env, obj, keyNapi, valueNapi);
    CHECK_AND_RETURN_RET_LOG(status == napi_ok, false, "failed to set property.");

    return true;
}

static bool SetPropertyString(napi_env env, napi_value &obj, const std::string &key, const std::string &value)
{
    SHARING_LOGD("trace.");
    CHECK_AND_RETURN_RET(obj != nullptr, false);
    napi_value keyNapi = nullptr;

    napi_status status = napi_create_string_utf8(env, key.c_str(), NAPI_AUTO_LENGTH, &keyNapi);
    CHECK_AND_RETURN_RET(status == napi_ok, false);

    napi_value valueNapi = nullptr;
    status = napi_create_string_utf8(env, value.c_str(), NAPI_AUTO_LENGTH, &valueNapi);
    CHECK_AND_RETURN_RET(status == napi_ok, false);

    status = napi_set_property(env, obj, keyNapi, valueNapi);
    CHECK_AND_RETURN_RET_LOG(status == napi_ok, false, "failed to set property.");

    return true;
}

napi_status WfdJsResult::GetJsResult(napi_env env, napi_value &result)
{
    SHARING_LOGD("trace.");
    napi_create_int32(env, ret_, &result);
    return napi_ok;
}

napi_status WfdErrorJsResult::GetJsResult(napi_env env, napi_value &result)
{
    SHARING_LOGD("trace.");
    napi_status status = napi_create_object(env, &result);
    if (status != napi_ok) {
        return status;
    }

    (void)ret_;

    if (!SetPropertyInt32(env, result, "code", code_)) {
        return napi_invalid_arg;
    }

    if (!SetPropertyString(env, result, "message", msg_)) {
        return napi_invalid_arg;
    }

    if (!SetPropertyString(env, result, "deviceId", deviceId_)) {
        return napi_invalid_arg;
    }

    return napi_ok;
}

napi_status WfdSourceDeviceFoundJsResult::GetJsResult(napi_env env, napi_value &result)
{
    SHARING_LOGD("trace.");
    napi_status status = napi_create_array_with_length(env, info_.size(), &result);
    if (status != napi_ok) {
        return status;
    }
    napi_value listObject;
    status = napi_create_object(env, &listObject);
    if (status != napi_ok) {
        return status;
    }

    (void)ret_;

    for (size_t i = 0; i < info_.size(); ++i) {
        if (!SetPropertyString(env, listObject, "deviceId", info_[i].deviceId)) {  
            return napi_invalid_arg;
        }

        if (!SetPropertyString(env, listObject, "deviceName", info_[i].deviceName)) {
            return napi_invalid_arg;
        }

        if (!SetPropertyString(env, listObject, "ipAddress", info_[i].ipAddr)) {
            return napi_invalid_arg;
        }

        if (!SetPropertyString(env, listObject, "networkId", "")) {
            return napi_invalid_arg;
        }
    
        if (!SetPropertyInt32(env, listObject, "isLeagacy", 1)) {
            return napi_invalid_arg;
        }
    
        if (!SetPropertyInt32(env, listObject, "deviceType", -1)) {
            return napi_invalid_arg;
        }
    
        if (!SetPropertyInt32(env, listObject, "subDeviceType", -1)) {
            return napi_invalid_arg;
        }
    
        if (!SetPropertyInt32(env, listObject, "channelType", -1)) {
            return napi_invalid_arg;
        }

        status = napi_set_element(env, result, i, listObject);
        if (status != napi_ok) {
            return status;
        }
    }

    return napi_ok;
}

napi_status WfdSinkeDeviceRequestJsResult::GetJsResult(napi_env env, napi_value &result)
{
    SHARING_LOGD("trace.");
    napi_status status = napi_create_array_with_length(env, info_.size(), &result);
    if (status != napi_ok) {
        return status;
    }
    napi_value listObject;
    status = napi_create_object(env, &listObject);
    if (status != napi_ok) {
        return status;
    }

    (void)ret_;

    // 遍历vector数组
    for (size_t i = 0; i < info_.size(); ++i) {
        if (!SetPropertyString(env, listObject, "deviceId", info_[i].deviceId)) {  
            return napi_invalid_arg;
        }
    
        if (!SetPropertyString(env, listObject, "deviceName", info_[i].deviceName)) {
            return napi_invalid_arg;
        }
    
        if (!SetPropertyString(env, listObject, "ipAddress", info_[i].ipAddr)) {
            return napi_invalid_arg;
        }

        status = napi_set_element(env, result, i, listObject);
        if (status != napi_ok) {
            return status;
        }
    }

    return napi_ok;
}

// napi_status WfdSourceAccelerationJsResult::GetJsResult(napi_env env, napi_value &result)
// {
//     SHARING_LOGD("trace.");
//     napi_status status = napi_create_string_utf8(env, surfaceId_.c_str(), NAPI_AUTO_LENGTH, &result);
//     if (status != napi_ok) {
//         return status;
//     }

//     (void)ret_;

//     return napi_ok;
// }

} // namespace Sharing
} // namespace OHOS